<?php

namespace App\Api\Controllers\Glow;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NeedController extends BaseController{
    
    public function get_stores(){
        $date_time=date("Y-m-d",strtotime("-1 day"));
        echo '<pre>';
        print_r(1212);
        die;
        $stores=DB::table('stores')->select('store_id')->get();
        foreach ($stores as $k=>$v){
            DB::table('store_day')->insert(array('settling_day'=>$date_time,'store_id'=>$v->store_id));
        }
    }
    
    //计划任务-汇总门店数据
    public function store_count(){
        $stores=DB::table('store_day')->select('store_id','settling_day')->limit(500)->get()->toArray();
        foreach ($stores as $k=>$v){
            //查询总店是否有分店
            $store=DB::table('stores')->where(array('store_id'=>$v->store_id))->select('pid','id')->first();
            if($store->pid>0){
                //获取总店门店id
                $sub_store=DB::table('stores')->where(array('id'=>$store->pid))->select('store_id')->first();
            }
            $id=$store->id;
            $where[] = ['orders.created_at', '>=', $v->settling_day.' 00:00:00'];
            $where[] = ['orders.created_at', '<=', $v->settling_day.' 23:59:59'];
            //门店汇总
            $stores_data = DB::table('orders')->join('stores', function ($join) {$join->on('orders.store_id', '=', 'stores.store_id');})->whereIn('orders.pay_status', [1, 6])->where($where)->where(function ($query) use($id) {$query->where('stores.id', $id)->orwhere('stores.pid',$id);return $query;})->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total'),DB::raw('sum(orders.refund_amount) as refund_amount'),DB::raw('sum(orders.fee_amount) as fee_amount')])->first();
                
            //退款汇总
            $stores_data_refund = DB::table('orders')->join('stores', function ($join) {$join->on('orders.store_id', '=', 'stores.store_id');})->whereIn('orders.pay_status', [6])->where($where)->where(function ($query) use($id) {$query->where('stores.id', $id)->orwhere('stores.pid',$id);return $query;})->select([DB::raw('count(orders.out_trade_no) as total')])->first();
            //微信汇总
            $stores_data_wx= DB::table('orders')->join('stores', function ($join) {
                $join->on('orders.store_id', '=', 'stores.store_id');
            })
                ->whereIn('orders.pay_status', [1,6])
                ->where('orders.ways_source', 'weixin')
                ->where($where)
                ->where(function ($query) use($id) {
                    $query->where('stores.id', $id)->orwhere('stores.pid',$id);
                    return $query;
                })
                ->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total')])
                ->first();
                
            //支付宝汇总
            $stores_data_zfb= DB::table('orders')->join('stores', function ($join) {
                $join->on('orders.store_id', '=', 'stores.store_id');
            })
                ->whereIn('orders.pay_status', [1,6])
                ->where('orders.ways_source', 'alipay')
                ->where($where)
                ->where(function ($query) use($id) {
                    $query->where('stores.id', $id)->orwhere('stores.pid',$id);
                    return $query;
                })
                ->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total')])
                ->first();
                
            //云闪付汇总
            $stores_data_uni= DB::table('orders')->join('stores', function ($join) {
                $join->on('orders.store_id', '=', 'stores.store_id');
            })
                ->whereIn('orders.pay_status', [1,6])
                ->where('orders.ways_source', 'unionpay')
                ->where($where)
                ->where(function ($query) use($id) {
                    $query->where('stores.id', $id)->orwhere('stores.pid',$id);
                    return $query;
                })
                ->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total')])
                ->first();
                
            //翼支付汇总
            $stores_data_wing= DB::table('orders')->join('stores', function ($join) {
                $join->on('orders.store_id', '=', 'stores.store_id');
            })
                ->whereIn('orders.pay_status', [1,6])
                ->where('orders.ways_source', 'wingpay')
                ->where($where)
                ->where(function ($query) use($id) {
                    $query->where('stores.id', $id)->orwhere('stores.pid',$id);
                    return $query;
                })
                ->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total')])
                ->first();
            $arr=array(
                'store_amount'=>$stores_data->total_amount-$stores_data->refund_amount,
                'refund_amount'=>$stores_data->refund_amount,
                'fee_amount'=>$stores_data->fee_amount,
                'store_order_count'=>$stores_data->total,
                'refund_order_num'=>$stores_data_refund->total,
                'store_id'=> $v->store_id,
                'sub_storeid'=>$store->pid==0 ? 0 :$upstore->store_id,
                'settling_day'=>$v->settling_day,
                'weixin_amount'=>$stores_data_wx->total_amount,
                'weixin_num'=>$stores_data_wx->total,
                'alipay_amount'=>$stores_data_zfb->total_amount,
                'alipay_num'=>$stores_data_zfb->total,
                'unionpay_amount'=>$stores_data_uni->total_amount,
                'unionpay_num'=>$stores_data_uni->total,
                'bestpay_amount'=>$stores_data_wing->total_amount?$stores_data_wing->total_amount:0,
                'bestpay_num'=>$stores_data_wing->total,
                'created_at'=>time()
            );
            DB::table('stores_day_count')->insert($arr);
            $merchant_stores = DB::table('merchant_stores')->where('store_id',$v->store_id)->select('merchant_id')->get();
            foreach($merchant_stores as $key=>$val){
            //收银员汇总
            $stores_data_merch= DB::table('orders')->join('stores', function ($join) {
                $join->on('orders.store_id', '=', 'stores.store_id');
            })
                ->where('orders.merchant_id', $val->merchant_id)
                ->whereIn('orders.pay_status', [1,6])
                ->where($where)
                ->where(function ($query) use($id) {
                    $query->where('stores.id', $id)->orwhere('stores.pid',$id);
                    return $query;
                })
                ->select([DB::raw('sum(orders.total_amount) as total_amount'),DB::raw('count(orders.out_trade_no) as total')])
                ->first();
                $mer=array(
                    'store_id'=>$v->store_id,
                    'merchant_id'=>$val->merchant_id,
                    'merchant_money'=>$stores_data_merch->total_amount?$stores_data_merch->total_amount:0,
                    'merchant_num'=>$stores_data_merch->total,
                    'created_at'=>time(),
                    'settling_day'=>$v->settling_day,
                );
                DB::table('stores_cash_count')->insert($mer);
            }
        }
    }
}